package com.zzyl.serve.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.RedisKeyConstants;
import com.zzyl.serve.domain.DeviceData;
import com.zzyl.serve.domain.Room;
import com.zzyl.serve.mapper.RoomMapper;
import com.zzyl.serve.service.IRoomService;
import com.zzyl.serve.vo.DeviceInfo;
import com.zzyl.serve.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author howstrong
 * @date 2025-03-03
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo getRoomById(Long id) {
        return roomMapper.getRoomById(id);
    }

    /**
     * 获取房间中的智能设备及数据
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        // 1. 查询mysql智能楼层的基础数据（房间、床位、设备（房间|床位））
        List<RoomVo> roomVos = roomMapper.getRoomAndBedByFloorId(floorId);

        // 2. 遍历房间数据，从redis获取房间设备所对应的设备上报的数据
        roomVos.forEach(roomVo->{

            roomVo.getDeviceVos().forEach(roomDeviceVo->{
                String iotId = roomDeviceVo.getIotId();
                List<DeviceData> deviceDataList = (List<DeviceData>) redisTemplate.opsForHash().get(RedisKeyConstants.IOT_DEVICE_LAST_DATA, iotId);

                roomDeviceVo.setDeviceDataVos(deviceDataList);

                // 3. 遍历床位数据，从redis获取床位对应的设备列表，获取设备上报的数据
                roomVo.getBedVoList().forEach(bedVo->{

                    bedVo.getDeviceVos().forEach(bedDeviceVo->{
                        String bedIotId = bedDeviceVo.getIotId();
                        List<DeviceData> bedList = (List<DeviceData>) redisTemplate.opsForHash().get(RedisKeyConstants.IOT_DEVICE_LAST_DATA, bedIotId);

                        bedDeviceVo.setDeviceDataVos(bedList);
                    });

                });

            });

        });
        return roomVos;
    }
}
